package com.lindroy.androidplayer.base.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.lindroy.androidplayer.interfaces.OnItemChildClickListener
import com.lindroy.androidplayer.utils.getViewBinding

/**
 * @author Lin
 * @date 2022/1/24
 * @function 无需分页的 Adapter
 */

private typealias  OnItemClickListener<T> = (view: View, item: T, position: Int) -> Unit
private typealias  OnItemLongClickListener<T> = (view: View, item: T, position: Int) -> Boolean

abstract class BaseListAdapter<T : Any, VB : ViewDataBinding>(private val owner: LifecycleOwner) :
    RecyclerView.Adapter<BaseListAdapter.ViewHolder<VB>>() {

    private var itemClickListener: OnItemClickListener<T>? = null

    private var itemLongClickListener: OnItemLongClickListener<T>? = null

    protected val itemList = arrayListOf<T>()

    fun setData(datas: List<T>) {
        DiffUtil.calculateDiff(object : DiffUtil.Callback() {
            override fun getOldListSize(): Int = itemList.size

            override fun getNewListSize(): Int = datas.size

            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                artItemsTheSame(itemList[oldItemPosition], datas[newItemPosition])

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
                areContentsTheSame(itemList[oldItemPosition], datas[newItemPosition])

        }).dispatchUpdatesTo(this)
        itemList.clear()
        itemList.addAll(datas)
    }

    fun getItem(position: Int) = itemList[position]

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder<VB> =
        getViewBinding<VB>(
            LayoutInflater.from(parent.context), parent, false, 1
        ).run {
            this.lifecycleOwner = owner
            ViewHolder(this)
        }

    override fun onBindViewHolder(holder: ViewHolder<VB>,  position: Int) {
        holder.binding.apply {
            onBindChildClick(object : OnItemChildClickListener<T> {
                override fun onClick(view: View, item: T) {
                    itemClickListener?.invoke(view, item, holder.absoluteAdapterPosition)
                }

                override fun onLongClick(view: View, item: T) =
                    itemLongClickListener?.invoke(view, item, holder.absoluteAdapterPosition) ?: false

            })
            onBindViewHolder(getItem(position), position)
            executePendingBindings()
        }
    }

    override fun getItemCount() = itemList.size

    class ViewHolder<VB : ViewDataBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root)

    abstract fun VB.onBindViewHolder(item: T, position: Int)

    abstract fun artItemsTheSame(oldItem: T, newItem: T): Boolean

    abstract fun areContentsTheSame(oldItem: T, newItem: T): Boolean

    open fun VB.onBindChildClick(itemClickListener: OnItemChildClickListener<T>) {
    }

    fun setOnChildClickListener(listener: OnItemClickListener<T>) {
        itemClickListener = listener
    }

    fun setOnChildLongClickListener(listener: OnItemLongClickListener<T>) {
        itemLongClickListener = listener
    }
}